The number 512 is interesting because it is equal to the sum of its digits raised to some power: 5 + 1 + 2 = 8, and 83 = 512. Another example of a number with this property is 614656 = 284.
We shall define an to be the nth term of this sequence and insist that a number must contain at least two digits to have a sum.
You are given that a2 = 512 and a10 = 614656.
Find a30.
In [1]:
MAX = 10**15
expt = 3
results = set()
for expt in range(2, 50):
for n in range(2, 10000):
power = n ** expt
if power > MAX or (expt == 2 and power > 10000):
break
if sum(map(int,str(power))) == n:
results.add(power)
print(sorted(results)[29])
Explanation: 81 is the only number (with at least two digits) that is equal to the square of the sum of its digits. Suppose that $n$ is a number with $d$ digits that is equal to the square of the sum of its digits. Then $10^{d-1} \le n < 10^d$ and $n \le (9d)^2$, which imply that $\displaystyle\frac{10^{d-1}}{d^2} \le 81$. This inequality is satisfied for $d \le 4$. Therefore $n < 10000$.
In [2]:
for d in range(1, 11):
print(d, 10**(d-1)/(d**2))
In [ ]: